public class Main {
    //合并两个有序链表
    public static MySingleList.Node mergeTwoLists(MySingleList.Node head1,MySingleList.Node head2){
        MySingleList.Node newHead = new MySingleList.Node(0); // 获得静态内部类对象
        MySingleList.Node tmp = newHead;
        while(head1!=null&&head2!=null){
            if(head1.val<head2.val){
                tmp.next = head1;
                head1 = head1.next;
                tmp = tmp.next;
            }else{
                tmp.next = head2;
                head2 = head2.next;
                tmp = tmp.next;
            }
        }
        if(head1!=null){  //说明head2走完了
            tmp.next = head1; // 所以把head1接到tmp的后面
            //谁不为空把谁串起来
        }
        if(head2!=null){  //说明head1走完了
            tmp.next = head2; // 所以把head2接到tmp后面
        }
        return newHead.next;
    }

    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(10);
        mySingleList.addLast(20);
        mySingleList.addLast(30);
        mySingleList.display();

        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(15);
        mySingleList2.addLast(25);
        mySingleList2.addLast(100);
        mySingleList2.display();

        System.out.println("====有序链表=====");
        MySingleList.Node ret =   mergeTwoLists(mySingleList.head,mySingleList2.head);
        mySingleList2.display(ret);
    }
    public static void main3(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.createLink();
        mySingleList.display();
        System.out.println("====尾插====");
        mySingleList.addLast(10);
        mySingleList.addLast(20);
        mySingleList.addLast(30);
        mySingleList.display();
        System.out.println("===倒数第K个节点=====");
        MySingleList.Node ret =  mySingleList.FindKthToTail(7);
        System.out.println(ret.val);

    }
    public static void main2(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.createLink();
        mySingleList.display();
        System.out.println("====尾插====");
        mySingleList.addLast(10);
        mySingleList.addLast(20);
        mySingleList.addLast(30);
        mySingleList.display();
        System.out.println("============");
        //链表的反转
        MySingleList.Node ret =   mySingleList.reverseList();
        // 从指定位置开始打印
        mySingleList.display(ret);
    }
    public static void main1(String[] args) {
        // 链表是一个Node类

        MySingleList mySingleList = new MySingleList();
        mySingleList.createLink();
        mySingleList.display();
        System.out.println("==========");
        mySingleList.display();
        boolean flg =  mySingleList.contains(0);
        System.out.println(flg);
        System.out.println(mySingleList.size());
        System.out.println("====尾插====");
        mySingleList.addLast(10);
        mySingleList.addLast(20);
        mySingleList.addLast(30);
        mySingleList.display();
        System.out.println("====头插====");
        mySingleList.addFirst(10);
        mySingleList.addFirst(20);
        mySingleList.addFirst(30);
        mySingleList.display();
        System.out.println("===========");
        mySingleList.addIndex(2,43);
        mySingleList.display();
        System.out.println("============");
        mySingleList.remove(10);
        mySingleList.remove(10);
        mySingleList.remove(30);
        mySingleList.remove(30);
        mySingleList.display();
        System.out.println("============");
        mySingleList.removeAllKey(20);
        mySingleList.display();
    }
}